package api;

import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求的编码，告诉servlet 以什么样的格式来理解请求
        req.setCharacterEncoding("utf8");
        //设置响应的编码，告诉servlet 以什么样的格式来构造响应
        //resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");

        //用户查询之后要进行一个数据之间的类型转换
        //然后需要进行把基本数据类型转换成引用数据类型
        //1.读取参数中的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (username == null || "".equals(username) || password == null ||
        "".equals(password)) {
            //登录失败！
            String html = "<h3>登录失败，缺少username字段 或者password字段！<h3/>";
            resp.getWriter().write(html);
            return;
        }
        //2.读数据库，看用户名是否存在，并且密码是否匹配
        UserDao userDao = new UserDao();
        User user = userDao.selectUserByName(username);
        if (user == null) {
            //用户不存在，
        }

    }
}
